/*
 * Copyright 2019 Zhenjie Yan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.caigourenyuan;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mingmen.canting.R;
import com.mingmen.canting.definerecycleview.ExpandableAdapter;
import com.mingmen.canting.dialog.ButtomDialogView;
import com.mingmen.canting.model.GongYingShangModel;
import com.mingmen.canting.model.Group;
import com.mingmen.canting.model.GroupMember;

import java.util.List;

/**
 * Created by Zhenjie Yan on 1/30/19.
 */
public class GongYingShangAdapter extends ExpandableAdapter<ExpandableAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private List<GongYingShangModel.DataBean> mGroupList;
    Context context;
    ButtomDialogView bottomSheetDialog;
    private int checkedPosition = -1;

    public interface OnItemClickListener {
        //        void onItemClickListener(int pos, DaishenheListAdapter.ViewName viewName, List<MyLiveList> myLiveList);
        void onItemClickListener(View view, int pos, List<GongYingShangModel.DataBean> groupList);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public GongYingShangAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.context = context;

    }

    public void setGroupList(List<GongYingShangModel.DataBean> groupList) {
        this.mGroupList = groupList;
        notifyDataSetChanged();
    }

    @Override
    public int parentItemCount() {
        return mGroupList == null ? 0 : mGroupList.size();
    }

    @Override
    public int childItemCount(int parentPosition) {
        List<GongYingShangModel.DataBean.DetailListBean> memberList = mGroupList.get(parentPosition).getDetailList();
        return memberList == null ? 0 : memberList.size();
    }

    @Override
    public ViewHolder createParentHolder(@NonNull ViewGroup root, int viewType) {
        View view = mInflater.inflate(R.layout.item_gongyingshang_expand_parent, root, false);
//        View view = mInflater.inflate(R.layout.item_expand_test, root, false);

        return new ParentHolder(view, this);
    }

    @Override
    public ViewHolder createChildHolder(@NonNull ViewGroup root, int viewType) {
//        View view = mInflater.inflate(R.layout.item_chushi_shenhe_child, root, false);
        View view = mInflater.inflate(R.layout.item_gongyingshang_child_expand, root, false);
//        View view = mInflater.inflate(R.layout.item_expand_child, root, false);
        return new ChildHolder(view, this);
    }


    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    @Override
    public void bindChildHolder(@NonNull ViewHolder holder, int parentPosition, int position) {
//        ((ChildHolder)holder).setData(mGroupList.get(parentPosition).getMemberList().get(position));
        ChildHolder myholder = (ChildHolder) holder;
//        myholder.setData(mGroupList.get(parentPosition).getMemberList().get(position));
        mGroupList.get(parentPosition).isExpend();
        int childsize = mGroupList.get(parentPosition).getDetailList().size();
//        shape_chushi_dingdan_ziitem3
        if (position == 0) {
            myholder.layout_first.setVisibility(View.VISIBLE);
            myholder.view_first.setVisibility(View.VISIBLE);
        } else {
            myholder.layout_first.setVisibility(View.GONE);
            myholder.view_first.setVisibility(View.GONE);
        }
        if (position == childsize - 1) {
//            myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem2));
        } else {
//            myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem3));
        }
//        myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem3));
        GongYingShangModel.DataBean.DetailListBean data = mGroupList.get(parentPosition).getDetailList().get(position);
        myholder.mTvTitle.setText(data.getCommodityName());


        myholder.specs.setText(data.getSpecs());
        myholder.price.setText(data.getPrice() + "");
    }

    static class ChildHolder extends ViewHolder {

        TextView mTvTitle;

        LinearLayout layout_item;
        LinearLayout layout_first;
        TextView specs;
        TextView price;
        View view_first;


        public ChildHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            layout_first = itemView.findViewById(R.id.layout_first);
            layout_item = itemView.findViewById(R.id.layout_item);
            specs = itemView.findViewById(R.id.specs);
            price = itemView.findViewById(R.id.price);
            view_first = itemView.findViewById(R.id.view_first);

        }

        public void setData(GroupMember data) {
            mTvTitle.setText(data.getName());
        }
    }


    @Override
    public void bindParentHolder(@NonNull ViewHolder holder, int position) {
//        ((ParentHolder) holder).setData(mGroupList.get(position));
        GongYingShangModel.DataBean data = mGroupList.get(position);
        ParentHolder parentHolder = (ParentHolder) holder;

        Log.d("ISEXPAndedisexpanded", data.isExpend() + "");
//        layout_parent

        if (data.isExpend() == true) {
            Matrix matrix = new Matrix();
            Bitmap bitmap = ((BitmapDrawable) context.getResources().getDrawable(R.mipmap.arrow_red)).getBitmap();
            // 设置旋转角度
            matrix.setRotate(90);
            // 重新绘制Bitmap
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            parentHolder.iv_status.setImageBitmap(bitmap);
        } else {
            parentHolder.iv_status.setImageResource(R.mipmap.arrow_red);
        }
        parentHolder.supplierName.setText(data.getSupplierName());

        parentHolder.mIvStatus.setSelected(data.isExpend());

        parentHolder.layout_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, mGroupList);
            }
        });
        parentHolder.btn_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, mGroupList);
            }
        });
        parentHolder.ivAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, mGroupList);
            }
        });
        parentHolder.btn_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, mGroupList);
            }
        });
//        parentHolder.tv_title.setText(data.getSupplierName());
        parentHolder.tv_title.setText("合作供应商");
        parentHolder.text_count.setText(data.getSalesAmount() + "");
        if (data.getStockState().equals("0")) {
            parentHolder.text_stockState.setText("全部有货");
        } else if (data.getStockState().equals("1")) {
            parentHolder.text_stockState.setText("部分有货");
        } else if (data.getStockState().equals("2")) {
            parentHolder.text_stockState.setText("全部无货");
        }
        parentHolder.text_address.setText("地址:" + data.getProvince() + data.getCity() + data.getRegion() + data.getStreet());
        parentHolder.ordersAmount.setText(data.getOrdersAmount() + "元");
        parentHolder.shippingFee.setText("另需配送费:  ￥" + data.getShippingFee() + "");
        parentHolder.score.setText(data.getScore() + "分");
    }

    static class ParentHolder extends ViewHolder {


        ImageView mIvStatus;
        LinearLayout layout_parent;
        LinearLayout layout_top;

        TextView tv_title;
        TextView text_count;
        TextView text_stockState;
        TextView text_address;
        TextView ordersAmount;
        TextView shippingFee;
        TextView btn_select;
        ImageView ivAvatar;
        TextView score;
        ImageView iv_status;
        TextView supplierName;

        Button btn_connect;

        public ParentHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);

            mIvStatus = itemView.findViewById(R.id.iv_status);
            layout_parent = itemView.findViewById(R.id.layout_parent);
            layout_top = itemView.findViewById(R.id.layout_top);
            tv_title = itemView.findViewById(R.id.tv_title);
            text_count = itemView.findViewById(R.id.text_count);
            text_stockState = itemView.findViewById(R.id.text_stockState);
            text_address = itemView.findViewById(R.id.text_address);
            ordersAmount = itemView.findViewById(R.id.ordersAmount);
            shippingFee = itemView.findViewById(R.id.shippingFee);
            btn_select = itemView.findViewById(R.id.btn_select);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            score = itemView.findViewById(R.id.score);
            iv_status = itemView.findViewById(R.id.iv_status);
            supplierName = itemView.findViewById(R.id.supplierName);
            btn_connect= itemView.findViewById(R.id.btn_connect);
        }


    }


}